/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package BLogic;

import POJOs.SEmailableUser;
import POJOs.SLogableUser;
import java.util.Properties;
import javax.mail.*;
import javax.mail.Authenticator;
import javax.mail.internet.*;

/**
 *
 * @author fabiofurlan
 */
public class VBaseMailUtil <USER extends SEmailableUser, MESSAGE extends RBasicEmailableMessage> {


     public void sendMail (USER mailSender, MESSAGE messages)
      throws MessagingException
  {

    System.out.println("VBaseMailUtil | sendMail | mail session creation");
    Properties props = new Properties();
    props.put("mail.smtp.host", mailSender.smtpProvider);
    props.put("mail.smtp.auth", "true");

    Authenticator auth=new SMTPAuthenticator();
    Session session = Session.getDefaultInstance(props, auth);

    System.out.println("VBaseMailUtil | sendMail | message creation");
    MimeMessage message = new MimeMessage(session);
    message.setSubject(messages.getSubject());
    message.setText(messages.getMessage());

    System.out.println("VBaseMailUtil | sendMail | addresses attach");
    InternetAddress fromAddress = new InternetAddress(mailSender.Email);
    InternetAddress toAddress = new InternetAddress(mailSender.mittEmail);
    message.setFrom(fromAddress);
    message.setRecipient(Message.RecipientType.TO, toAddress);

    System.out.println("VBaseMailUtil | sendMail | SENDING MAIL");
    Transport.send(message);
  }

}
